{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "# Using ReliableTool to Generate Sub-Questions (Synchronous Version)\n",
    "This notebook demonstrates how to use the `ReliableTool` synchronously."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import logging\n",
    "import os\n",
    "from typing import Any\n",
    "\n",
    "from autogen import LLMConfig, config_list_from_json\n",
    "from autogen.tools.experimental.google_search import GoogleSearchTool\n",
    "from autogen.tools.experimental.reliable import ReliableTool\n",
    "\n",
    "logging.basicConfig(level=logging.INFO, format=\"%(asctime)s [%(levelname)s] %(name)s: %(message)s\")\n",
    "logger = logging.getLogger(__name__)\n",
    "\n",
    "print(\"Successfully imported components from autogen.\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "2",
   "metadata": {},
   "source": [
    "\n",
    "## 1. Define the Google Search Tool\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [],
   "source": [
    "search_api_key = os.getenv(\"GOOGLE_SEARCH_API_KEY\")\n",
    "search_engine_id = os.getenv(\"GOOGLE_SEARCH_ENGINE_ID\")\n",
    "\n",
    "gs_tool: GoogleSearchTool = GoogleSearchTool(\n",
    "    search_api_key=search_api_key,\n",
    "    search_engine_id=search_engine_id,\n",
    ")\n",
    "logger.info(\"GoogleSearchTool instance created successfully.\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "4",
   "metadata": {},
   "source": [
    "## 2. Configure LLM and ReliableTool"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "metadata": {},
   "outputs": [],
   "source": [
    "llm_config: LLMConfig | None = None\n",
    "config_list = config_list_from_json(\n",
    "    \"OAI_CONFIG_LIST\",\n",
    ")\n",
    "llm_config = LLMConfig(\n",
    "    *config_list,\n",
    "    temperature=0.7,\n",
    ")\n",
    "\n",
    "system_message_addition_for_tool_calling = \"\"\"You are an assistant that prepares search queries for the Google Search tool.\n",
    "Your goal is to take a user request (the 'task') and formulate the most effective search query string for the tool to execute.\n",
    "The internal tool to call is named 'execute_google_search'.\n",
    "You MUST provide the search query as the 'query' argument to this tool.\n",
    "You MUST also provide a 'hypothesis' argument summarizing the type of information or answer expected from the search results (e.g., \"a list of recent news articles\", \"a definition\", \"the official website\").\n",
    "\n",
    "Example:\n",
    "User Task: \"Find recent news about the AutoGen library.\"\n",
    "Your Tool Call should be: execute_google_search(query=\"AutoGen library recent news\", hypothesis=\"A list of recent news articles or blog posts about the AutoGen library.\")\n",
    "\n",
    "Example:\n",
    "User Task: \"What is the capital of France?\"\n",
    "Your Tool Call should be: execute_google_search(query=\"capital of France\", hypothesis=\"The name of the capital city of France.\")\n",
    "\n",
    "You MUST invoke the 'execute_google_search' tool exactly once per response using a tool call format. Do not add conversational text.\n",
    "\"\"\"\n",
    "\n",
    "system_message_addition_for_result_validation = (\n",
    "    \"Make sure all search results are specifically from the link github.com.\"\n",
    ")\n",
    "\n",
    "reliable_search_tool: ReliableTool = ReliableTool(\n",
    "    name=\"ReliableGoogleSearch\",\n",
    "    func_or_tool=gs_tool,  # Pass the initialized GoogleSearchTool\n",
    "    description=\"Reliably performs a Google search based on a task description and validates the relevance of the results.\",\n",
    "    runner_llm_config=llm_config,\n",
    "    validator_llm_config=llm_config,\n",
    "    system_message_addition_for_tool_calling=system_message_addition_for_tool_calling,\n",
    "    system_message_addition_for_result_validation=system_message_addition_for_result_validation,\n",
    "    max_tool_invocations=4,\n",
    ")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "6",
   "metadata": {},
   "source": [
    "## 3. Get User Input and Run the Tool Synchronously"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "search_task = \"What is the latest news about the AutoGen project?\"\n",
    "\n",
    "logger.info(f\"\\nAttempting to reliably perform search task: '{search_task}'\")\n",
    "print(\"-\" * 40)\n",
    "\n",
    "result: Any | None = reliable_search_tool.run(\n",
    "    task=search_task, validation_prompt_addition=\"Ensure every link comes from github.\"\n",
    ")\n",
    "\n",
    "print(\"-\" * 40)\n",
    "logger.info(\"\\n✅ Successfully performed search task:\")\n",
    "print(f\"   Result Type: {type(result)}\")\n",
    "print(f\"   Result: {result}\")"
   ]
  }
 ],
 "metadata": {
  "front_matter": {
   "description": "ReliableTool - Google Search Example",
   "tags": [
    "tool calling",
    "tools",
    "reliable",
    "search",
    "google"
   ]
  },
  "kernelspec": {
   "display_name": "venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
